package com.imooc.service.impl;

import com.imooc.mapper.StuMapper;
import com.imooc.pojo.Stu;
import com.imooc.service.StuService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service
@AllArgsConstructor
public class StuServiceImpl implements StuService {

  public final StuMapper stuMapper;

  @Transactional(rollbackFor = Exception.class, propagation = Propagation.SUPPORTS)
  @Override
  public Stu getStuInfo(int id) {
    return stuMapper.selectById(id);
  }

  @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
  @Override
  public void saveStu() {
    Stu stu = new Stu();
    stu.setName("jack");
    stu.setAge(19);
    stuMapper.insert(stu);
  }

  @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
  @Override
  public void updateStu(int id) {
    Stu stu = new Stu();
    stu.setId(id);
    stu.setName("lucy");
    stu.setAge(20);
    stuMapper.updateById(stu);
  }

  @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
  @Override
  public void deleteStu(int id) {
    stuMapper.deleteById(id);
  }


  @Override
  public void saveParent() {
    Stu stu = new Stu();
    stu.setName("parent");
    stu.setAge(19);
    stuMapper.insert(stu);
  }

  @Override
  @Transactional(propagation = Propagation.NESTED)
  public void saveChildren() {
    saveChild1();
    int a = 1 / 0;
    saveChild2();
  }

  public void saveChild1() {
    Stu stu1 = new Stu();
    stu1.setName("child-1");
    stu1.setAge(11);
    stuMapper.insert(stu1);
  }

  public void saveChild2() {
    Stu stu2 = new Stu();
    stu2.setName("child-2");
    stu2.setAge(22);
    stuMapper.insert(stu2);
  }
}